package com.wtwd.device.filter;


import com.alibaba.fastjson.JSONObject;
import com.wtwd.device.access.AccessFactory;
import com.wtwd.device.access.SignatureAccess;
import com.wtwd.device.access.WebAccessAdapter;
import com.wtwd.device.entity.RespEntity;
import com.wtwd.device.entity.VerifyResult;
import com.wtwd.device.service.GatewayRedisService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;


/**
 * @author mjy
 * @Description
 * @create 2021-08-15 19:14
 */
@Component
public class AccessFilter implements GlobalFilter, Ordered {

    private static final String WEB_URL = "/web";

    public static final String IS_ACCESS_FIELD = "isAccessed";
    public static final String IS_ACCESSED = "1";


    @Autowired
    GatewayRedisService redisService;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 获取当前请求
        String path = exchange.getRequest().getPath().value();
        ServerHttpResponse response = exchange.getResponse();
        ServerHttpRequest request = exchange.getRequest();
        // 设置编码格式
        AccessFactory accessFactory = null;
        //第三方约定签名认证
        if (StringUtils.isNotBlank(request.getHeaders().getFirst(SignatureAccess.SIGN_FIELD))) {
            VerifyResult signVerifyResult = new SignatureAccess(request).verifyEncrypt();
            if (signVerifyResult == null) {
                response.getHeaders().add(IS_ACCESS_FIELD,IS_ACCESSED);
            }
        }
        if (path.startsWith(WEB_URL)) {
            //放开静态资源
            if (path.contains("/web/user/login") || path.contains(".xlsx") || path.contains(".xls") || path.contains(".png") || path.contains(".jpg")
                    || path.contains(".jpeg ") || path.contains("importDeviceInfo") || path.contains("uploadPreviewImage") || path.contains("uploadDialFile") || path.contains("uploadUpgradeFile") || path.contains("uploadImage")||path.contains("uploadPortrait")
                    ||path.contains("file-service")
            ) {
                chain.filter(exchange);
            } else {
                accessFactory = new WebAccessAdapter(request);
            }
        }
        if (accessFactory != null) {//暂时只做web端校验
            VerifyResult verifyResult = accessFactory.verifyEncrypt(request, redisService);
            if (verifyResult != null) {
                if (!verifyResult.isResult()) {
                    String rest = JSONObject.toJSONString(new RespEntity(verifyResult.getRespCode()));
                    DataBuffer buffer = response.bufferFactory().wrap(rest.getBytes(StandardCharsets.UTF_8));
                    return response.writeWith(Mono.just(buffer));
                }
            }
        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 1;
    }
}
